// <copyright file="FileSize.cs" company="Patrick S. Seymour">
// Copyright (c) Patrick S. Seymour. All rights reserved.
// </copyright>

namespace SftExaminer
{
    using System;

    /// <summary>
    /// This class contains a function to convert a file's size in bytes to
    /// a user-friendly string that indicates the size in kilobytes (KB),
    /// megabytes (MB), etc., whichever is appropriate.
    /// </summary>
    public class FileSize
    {/*
        public static string AutoFormat(double number)
        {
            return AutoFormat(number, 2);
        }

        public static string AutoFormat(double number, int decimalPlaces)
        {
        }
      */

        /// <summary>
        /// Converts a file size in bytes to a string containing the size
        /// in kilobytes (KB), megabytes (MB), etc., whichever is appropriate.
        /// </summary>
        /// <param name="number">The file size, in bytes, to be converted.</param>
        /// <returns>
        /// Returns a string containing a user-friendly representation of the file's size.
        /// </returns>
        /// <remarks>
        /// The converted file size will be rounded to two decimal places.
        /// </remarks>
        public static string AutoFormat(long number)
        {
            return AutoFormat(number, 2);
        }

        /// <summary>
        /// Converts a file size in bytes to a string containing the size
        /// in kilobytes (KB), megabytes (MB), etc., whichever is appropriate.
        /// </summary>
        /// <param name="number">The file size, in bytes, to be converted.</param>
        /// <param name="decimalPlaces">The number of decimal places to which the converted file size will be rounded.</param>
        /// <returns>
        /// Returns a string containing a user-friendly representation of the file's size.
        /// </returns>
        public static string AutoFormat(long number, int decimalPlaces)
        {
            float convertedNumber;

            System.Collections.Generic.SortedList<byte, string> abbrevList = new System.Collections.Generic.SortedList<byte, string>(4);

            abbrevList.Add(1, "KB");    // kilobyte
            abbrevList.Add(2, "MB");    // megabyte
            abbrevList.Add(3, "GB");    // gigabyte
            abbrevList.Add(4, "TB");    // terabyte
            abbrevList.Add(5, "PB");    // petabyte
            abbrevList.Add(6, "EB");    // exabyte

            for (int i = abbrevList.Count - 1; i >= 0; i--)
            {
                long powerOfTwo = (long)Math.Pow(2, abbrevList.Keys[i] * 10);
                if (number >= powerOfTwo)
                {
                    convertedNumber = (float)number / powerOfTwo;
                    return string.Format("{0} {1}", convertedNumber.ToString(string.Format("N{0}", decimalPlaces), System.Globalization.CultureInfo.CurrentUICulture), abbrevList.Values[i]);
                }
            }

            return string.Format("{0} bytes", number.ToString("N0", System.Globalization.CultureInfo.CurrentUICulture));
        }
    }
}
